<template>
  <div id="role-edit">
    <el-form ref="roleForm" :model="roleForm" label-width="100px">
      <el-form-item label="角色名称">
        <el-input v-model="roleForm.roleName" :disabled="editType === 'edit'"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="roleForm.roleDesc"></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch
          v-model="roleForm.isActive"
          active-text="是"
          inactive-text="否"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'RoleEdit',
  props: {
    roleInfo: {
      type: Object,
      require: true,
      default: () => {
        return {}
      }
    },
    editType: {
      type: String,
      required: true,
      validator(val) {
        return ['edit', 'add'].includes(val)
      }
    }
  },
  emit: ['cancel-edit', 'submit-edit'],
  data() {
    return {
      roleForm: {
        roleName: '',
        roleDesc: '',
        isActive: ''
      }
    }
  },
  watch: {
    roleInfo: {
      immediate: true,
      deep: true,
      handler(newVal) {
        if (newVal) {
          this.roleForm = JSON.parse(JSON.stringify(newVal))
        }
      }
    }
  },
  methods: {
    onSubmit() {
      this.$emit('submit-edit', this.roleForm, 'role')
    },
    onCancel() {
      this.$emit('cancel-edit')
    }
  }
}
</script>

<style scoped>

</style>
